package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.common.ResultPage;
import com.pwt.entity.Curves;
import com.pwt.model.CurvesHistoryModel;
import com.pwt.model.CurvesModel;
import com.pwt.model.ResultCurvesModel;
import com.pwt.model.TreeComBoxModel;
import com.pwt.model.what.CharacteristicsModel;
import com.pwt.model.what.HeaderModel;
import com.pwt.model.what.OverviewModel;
import com.pwt.model.what.WhatDetailsModel;
import com.pwt.repository.CurvesRepository;
import com.pwt.repository.WERTEVARRepository;
import com.pwt.utils.RgbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Curves service层
 *
 * @author auto generated
 * @date 2022-01-11 17:31:21
 */
@Service
public class CurvesService {

    private WERTEVARRepository wertevarRepository;

    private CurvesRepository curvesRepository;

    public CurvesService(WERTEVARRepository wertevarRepository, CurvesRepository curvesRepository) {
        this.wertevarRepository = wertevarRepository;
        this.curvesRepository = curvesRepository;
    }


    public Integer hasCurves(String k0014,Integer k0010){
       return this.wertevarRepository.hasCurvesFile(k0014,k0010);
    }


    public WhatDetailsModel<CharacteristicsModel> curvesOneTable(String k0014, Integer k0010, Integer page, Integer limit){
        HeaderModel headerModel= this.wertevarRepository.getHeader(k0014,k0010);
        if(headerModel == null) return null;
        String msg = this.wertevarRepository.hasError(k0014,k0010);
        if(StringUtils.isNotBlank(msg)){
            headerModel.setK0001(0);
            headerModel.setK0005(msg);
        }else {
            headerModel.setK0001(1);
        }
        ResultPage<CharacteristicsModel> resultPage = this.wertevarRepository.getCharacteristicsByK0014( k0014,  k0010, page, limit);
        WhatDetailsModel whatDetailsModel = new WhatDetailsModel();
        whatDetailsModel.setHeaderModel(headerModel);
        whatDetailsModel.setResultPage(resultPage);
        return whatDetailsModel;
    }


    public ResultPage<CurvesHistoryModel> queryHistoryCurves(String k0014, Integer k0010, Integer page, Integer limit){
        return this.curvesRepository.queryCurvesHistory(k0010,k0014, page, limit);
    }

    public List<TreeComBoxModel> queryTreeList(String begin,String end,Integer k0010){
        List<TreeComBoxModel> k1000List = this.curvesRepository.queryK1000AndTime(begin, end, k0010);
        if(k1000List == null && k1000List.size() < 1) return null;
        for (TreeComBoxModel com:k1000List){
            com.setList(this.curvesRepository.queryK0014AndTime(k0010,Integer.parseInt(com.getId().toString()),begin,end));
        }
        return k1000List;
    }


    public List<CurvesModel> curvesOne(String k0014, Integer k0010){

        List<CharacteristicsModel> list = this.wertevarRepository.getCharacteristicsByK0014AndK0010(k0014,k0010);
        if(list == null || list.size() != 2) return null;
        List<CurvesModel> curvesList = new ArrayList<>();


        for (int j=0;j<list.size();j++){
            CurvesModel curvesModels = new CurvesModel();
            if(j==0) curvesModels.setColor("#ff7f0e");
            else curvesModels.setColor("#1f77b4");

            curvesModels.setK0014("时间-"+list.get(j).getK2002());
            curvesModels.setXUnit("时间(s)");
            curvesModels.setYUnit(list.get(j).getK2002()+"("+list.get(j).getK2142()+")");
            List<String> x=new ArrayList<>();
            List<String> y=new ArrayList<>();
            String code = list.get(j).getCode();
            String curvesPoint = this.curvesRepository.queryCurvesByFilename(code);
            if(StringUtils.isBlank(curvesPoint)) return null;
            if(StringUtils.isNotBlank(curvesPoint)){
               String[] times = curvesPoint.split(";");
                for (int i = 0; i < times.length; i++) {
                    String[] units = times[i].split(",");
                    x.add(units[0]);
                    y.add(units[1]);
                }
            }
            curvesModels.setX(x);
            curvesModels.setY(y);
            curvesList.add(curvesModels);
        }
        return curvesList;
    }

    public List<ResultCurvesModel> curvesList(List<String> k0014, Integer k0010) throws CustomException {
        if(k0014 == null || k0014.size() < 1) throw new CustomException("零件编号不能为空！");
        List<ResultCurvesModel>  list32 = new ArrayList<>();

        List<CharacteristicsModel> list = this.wertevarRepository.getCharacteristicsByK0014AndK0010(k0014.get(0),k0010);
        if(list == null || list.size() < 1) return null;

        for (int a=0;a<list.size();a++){
            List<Map<String,Object>> ListCurves = this.curvesRepository.queryCurvesByCodeList(list.get(a).getK1000(),list.get(a).getK2000(),list.get(a).getDateTime(),k0014);
            if(ListCurves == null && ListCurves.size() < 1) continue;
            ResultCurvesModel resultCurvesModel = new ResultCurvesModel();
            resultCurvesModel.setK2000(list.get(a).getK2000());
            resultCurvesModel.setK2002(list.get(a).getK2002());
            //一条曲线
            List<CurvesModel> curvesList = new ArrayList<>();
            for (int j = 0; j < ListCurves.size(); j++) {
                String curvesPoint = ListCurves.get(j).get("PointData").toString();
                if(StringUtils.isBlank(curvesPoint)) continue;
                CurvesModel curvesModels = new CurvesModel();
                if(a==0) curvesModels.setColor("#ff7f0e");
                else curvesModels.setColor("#1f77b4");
                curvesModels.setK0014("时间-"+list.get(a).getK2002()+"-"+(j+1));
                curvesModels.setXUnit("时间(s)");
                curvesModels.setYUnit(list.get(a).getK2002()+"("+list.get(a).getK2142()+")");
                List<String> x=new ArrayList<>();
                List<String> y=new ArrayList<>();

                if(StringUtils.isNotBlank(curvesPoint)){
                    String[] times = curvesPoint.split(";");
                    for (int i = 0; i < times.length; i++) {
                        String[] units = times[i].split(",");
                        x.add(units[0]);
                        y.add(units[1]);
                    }
                }
                curvesModels.setX(x);
                curvesModels.setY(y);
                curvesList.add(curvesModels);
            }
            resultCurvesModel.setList(curvesList);
            list32.add(resultCurvesModel);
        }
        return list32;
    }

    public CurvesModel curvesForceAndStroke(String k0014, Integer k0010){
        List<CharacteristicsModel> list = this.wertevarRepository.getCharacteristicsByK0014AndK0010(k0014,k0010);
        if(list == null || list.size() != 2) return null;
        String curvesPoint = this.curvesRepository.queryCurvesByFilename(list.get(0).getCode());
        String rcurvesPoint = this.curvesRepository.queryCurvesByFilename(list.get(1).getCode());
        if(StringUtils.isBlank(curvesPoint) || StringUtils.isBlank(rcurvesPoint)) return null;
        //一条曲线
        CurvesModel curvesModels = new CurvesModel();
        curvesModels.setColor("#1f77b4");
        curvesModels.setK0014(k0014);
        curvesModels.setXUnit(list.get(0).getK2002()+"("+list.get(0).getK2142()+")");
        curvesModels.setYUnit(list.get(1).getK2002()+"("+list.get(1).getK2142()+")");

        List<String> x=new ArrayList<>();
        List<String> y=new ArrayList<>();
        if(StringUtils.isNotBlank(curvesPoint) && StringUtils.isNotBlank(rcurvesPoint)){
            String[] times = curvesPoint.split(";");
            String[] rtimes = rcurvesPoint.split(";");
            for (int i = 0; i < times.length; i++) {
                String[] units = times[i].split(",");
                String[] runits = rtimes[i].split(",");
                x.add(units[1]);
                y.add(runits[1]);
            }
            curvesModels.setX(x);
            curvesModels.setY(y);
        }


        return curvesModels;
    }

    public List<CurvesModel> curvesForceAndStrokeList(List<String> k0014, Integer k0010) throws CustomException {
        if(k0014 == null || k0014.size() < 1) throw new CustomException("零件编号不能为空！");
        List<CurvesModel> result = new ArrayList<>();

        List<CharacteristicsModel> list = this.wertevarRepository.getCharacteristicsByK0014AndK0010(k0014.get(0),k0010);
        if(list == null || list.size() < 2) return null;
        List<Map<String,Object>> ListCurvesx = this.curvesRepository.queryCurvesByCodeList(list.get(0).getK1000(),list.get(0).getK2000(),list.get(0).getDateTime(),k0014);
        List<Map<String,Object>> ListCurvesy = this.curvesRepository.queryCurvesByCodeList(list.get(1).getK1000(),list.get(1).getK2000(),list.get(1).getDateTime(),k0014);
        if(ListCurvesx == null || ListCurvesx.size() < 1) return null;
        if(ListCurvesy == null || ListCurvesy.size()< 1) return null;
        for (int i = 0; i < ListCurvesx.size(); i++) {
            //if(ListCurvesx.size()!= ListCurvesy.size()) throw new CustomException("数组长度不一致！");
            if(!ListCurvesx.get(i).get("WV0014").equals(ListCurvesy.get(i).get("WV0014"))) continue;
            String id = ListCurvesx.get(i).get("WV0014").toString();
            String curvesPoint = ListCurvesx.get(i).get("PointData").toString();
            if(StringUtils.isBlank(curvesPoint)) continue;
            String rcurvesPoint = ListCurvesy.get(i).get("PointData").toString();
            if(StringUtils.isBlank(rcurvesPoint)) continue;
            //一条曲线
            CurvesModel curvesModels = new CurvesModel();
            curvesModels.setK0014(id);
            curvesModels.setColor(RgbUtils.getRandColorCode());
            curvesModels.setXUnit(list.get(0).getK2002()+"("+list.get(0).getK2142()+")");
            curvesModels.setYUnit(list.get(1).getK2002()+"("+list.get(1).getK2142()+")");

            List<String> x=new ArrayList<>();
            List<String> y=new ArrayList<>();
            if(StringUtils.isNotBlank(curvesPoint) && StringUtils.isNotBlank(rcurvesPoint)){
                String[] times = curvesPoint.split(";");
                String[] rtimes = rcurvesPoint.split(";");
                for (int j = 0; j < times.length; j++) {
                    String[] units = times[j].split(",");
                    String[] runits = rtimes[j].split(",");
                    x.add(units[1]);
                    y.add(runits[1]);
                }
                curvesModels.setX(x);
                curvesModels.setY(y);
            }
            result.add(curvesModels);
        }


        return result;
    }

}
